package com.im.dynamic.controller;

import com.alibaba.fastjson.JSON;
import com.im.common.utils.R;
import com.im.dynamic.entity.Album;
import com.im.dynamic.entity.Post;
import com.im.dynamic.entity.User;
import com.im.dynamic.service.AlbumService;
import com.im.dynamic.service.PostService;
import com.im.dynamic.service.UserService;
import com.im.dynamic.utils.JWTUtils;
import com.im.dynamic.vo.DynamicVo;
import com.im.dynamic.vo.PublishVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.GeoOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@RestController
public class DynamicController {

    @Autowired
    private UserService userService;

    @Autowired
    private AlbumService albumService;

    @Autowired
    private PostService postService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查动态列表的接口
     */
    @GetMapping("/article/lists")
    public List<DynamicVo> lists(@RequestParam("page") Integer page,
                                 @RequestParam(value = "nearby", required = false) Integer nearby,
                                 @RequestParam(value = "keyword", required = false) String keyword,
                                 HttpServletRequest request) {
        GeoOperations<String, String> geo = redisTemplate.opsForGeo();
        String userInfo = request.getHeader("userInfo");
        User user1 = (User) JSON.parse(userInfo);
        Long id = user1.getId();

        //判断请求是否是查同城的请求
//        if (nearby == 1) {
//        }
        //判断请求是否是进行搜索的请求
//        if (keyword != null && !"".trim().equals(keyword)) {
//        }
        //判断是否同城且搜索
//        if (nearby == 1 && !"".equals(keyword)) {
//        }
        List<DynamicVo> dynamicVos = new ArrayList<>();
        List<Post> postList = postService.list();
        for (Post post : postList) {
            User user = userService.getById(post.getUId());
            Album album = albumService.getById(post.getUId());
            DynamicVo dynamicVo = new DynamicVo();
            dynamicVo.setUser(user);
            dynamicVo.setText(post.getContent());
            dynamicVo.setId(post.getId());
            dynamicVo.setType(post.getType());
            dynamicVo.setPath(album.getPath());
            dynamicVo.setPraiseNum(post.getPraiseNum());
            dynamicVo.setCommentNum(post.getCommentNum());
            dynamicVo.setCreateTime(post.getCreateTime());
        }
        return dynamicVos;
    }

    /**
     * 删除动态的接口
     */
    @PostMapping("/article/del")
    public R del() {
        return R.seccess("删除成功");
    }

    /**
     * 发布动态的接口
     */
    @PostMapping("/article/publish")
    public R publish(@RequestBody PublishVo publishVo) {
        //获取当前登录用户的Token，从Token中获取用户ID
        String userInfo = JWTUtils.getUserInfo("userInfo");
        User parse = (User) JSON.parse(userInfo);
        Long id = parse.getId();
        //将用户ID和用户所写的动态内容进行绑定
        Integer type = publishVo.getType();
        String[] image = publishVo.getImage();
        String text = publishVo.getText();
        Album album = new Album();
        album.setPath(String.valueOf(image));
        albumService.save(album);
        Post post = new Post();
        post.setType(type);
        post.setUId(Math.toIntExact(id));
        post.setCreateTime(new Date());
        post.setContent(text);
        postService.save(post);
        //分别将数据写入数据库
        //返回响应给前端
        return R.seccess("发布成功");
    }


    /**
     * 获取当前查看的用户的动态
     */
    @GetMapping("/article/list")
    public List<Post> list(HttpServletRequest request) {
        String userInfo = request.getHeader("userInfo");
        User user = (User) JSON.parse(userInfo);
        Long id = user.getId();

        List<Post> list = postService.getByUserId(id);

        List<DynamicVo> dynamicVos = new ArrayList<>();
        List<Post> postList = postService.list();

        return postList;
    }
}
